package com.pg.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pg.entity.dd.EipPurchaseOrder;
import com.pg.mapper.EipPurchaseOrderMapper;
import com.pg.service.EipPurchaseOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 采购订单 服务实现类
 * </p>
 *
 * @author likl
 * @since 2020-07-06
 */
@Service
@DS("proxyDs")
@Primary
public class EipPurchaseOrderServiceImpl extends ServiceImpl<EipPurchaseOrderMapper, EipPurchaseOrder> implements EipPurchaseOrderService {

    @Autowired
    private EipPurchaseOrderMapper eipPurchaseOrderMapper;

    @Override
    public List<Map<String, Object>> selectMapData(String supplierCode) {
        String[] supplierCodeArray = supplierCode.split("-");
        List<Map<String, Object>> list = eipPurchaseOrderMapper.selectContractProvinceTotal(supplierCodeArray, null);
        // 省份统计
        if (!CollectionUtils.isEmpty(list)) return getProvinceTotal(list);
        return null;
    }

    @Override
    public List<Map<String, Object>> selectByParam(String param, String supplierCode, Date sellerSignTime) {
        String[] supplierCodeArray = supplierCode.split("-");
        return eipPurchaseOrderMapper.selectByParam(param, supplierCodeArray, sellerSignTime);
    }

    @Override
    public List<Integer> selectContractStatisticsByDate(Date sellerSignTime) {
        List<Map<String, Integer>> list = eipPurchaseOrderMapper.selectContractStatisticsByDate(sellerSignTime);
        List<Integer> companyTotal = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (Map<String, Integer> map : list) {
                if ("1000014563".equals(map.get("supplierCode"))) {
                    if (map.get("count") == null) {
                        companyTotal.add(0);
                    } else {
                        companyTotal.add(map.get("count"));
                    }
                }
                if ("1000015019".equals(map.get("supplierCode"))) {
                    if (map.get("count") == null) {
                        companyTotal.add(0);
                    } else {
                        companyTotal.add(map.get("count"));
                    }
                }
            }
        }
        return companyTotal;
    }

    @Override
    public Map<String, List<Object>> selectContractProvinceTotal(String supplierCode, Date sellerSignTime) {
        String[] supplierCodeArray = supplierCode.split("-");
        List<Map<String, Object>> list = eipPurchaseOrderMapper.selectContractProvinceTotal(supplierCodeArray,
                sellerSignTime);
        List<String> company = new ArrayList<>();
        Map<String, List<Object>> resultMap = new HashMap<String, List<Object>>();

        List<Long> companyTotal = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            // 省份统计
            List<Map<String, Object>> ListTemp = getProvinceTotal(list).stream().sorted(new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Long value1 = (Long) o1.get("value");
                    Long value2 = (Long) o2.get("value");
                    return value2.compareTo(value1);
                }
            }).collect(Collectors.toList());

            for (Map<String, Object> map : ListTemp) {
                company.add((String) map.get("name"));
                companyTotal.add((Long) map.get("value"));
            }
        }

        if (supplierCode.contains("-")) {
            Collections.reverse(company);
            Collections.reverse(companyTotal);
        }
        resultMap.put("province", Collections.singletonList(company));
        resultMap.put("provinceTotal", Collections.singletonList(companyTotal));
        return resultMap;
    }

    private List<Map<String, Object>> getProvinceTotal(List<Map<String, Object>> list) {
        List<Map<String, Object>> listTemp = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : list) {
            Map<String, Object> mapTemp = new HashMap<>();
            if (!"蒙东".equals(map.get("buyerName")) && !"蒙西".equals(map.get("buyerName")) && !"内蒙古".equals(map.get("buyerName")) && !"冀北".equals(map.get("buyerName")) && !"北京".equals(map.get("buyerName"))) {
                mapTemp.put("name", map.get("buyerName"));
                mapTemp.put("value", map.get("count"));
                mapTemp.put("selected", true);
                listTemp.add(mapTemp);
            }
        }
        Long neiMengGuSum = list.stream().filter(item -> "蒙东".equals(item.get("buyerName")) || "蒙西".equals(item.get("buyerName")) || "内蒙古".equals(item.get("buyerName"))).mapToLong(item -> (Long) item.get("count")).sum();
        if (neiMengGuSum > 0) {
            Map<String, Object> mapTemp = new HashMap<>();
            mapTemp.put("name", "内蒙古");
            mapTemp.put("value", neiMengGuSum);
            mapTemp.put("selected", true);
            listTemp.add(mapTemp);
        }
        Long beiJingSum = list.stream().filter(item -> "冀北".equals(item.get("buyerName")) || "北京".equals(item.get("buyerName"))).mapToLong(item -> (Long) item.get("count")).sum();
        if (neiMengGuSum > 0) {
            Map<String, Object> mapTemp = new HashMap<>();
            mapTemp.put("name", "北京");
            mapTemp.put("value", beiJingSum);
            mapTemp.put("selected", true);
            listTemp.add(mapTemp);
        }
        return listTemp;
    }

    @Override
    public IPage<EipPurchaseOrder> selectForList(Page<EipPurchaseOrder> page, EipPurchaseOrder eipPurchaseOrder, Date sellerSignTimeStart, Date sellerSignTimeEnd) {
        LambdaQueryWrapper<EipPurchaseOrder> queryWrapper = new LambdaQueryWrapper<>();
        // 采购订单号
        if (StringUtils.isNotEmpty(eipPurchaseOrder.getSupplierCode())) {
            queryWrapper.like(EipPurchaseOrder::getSupplierCode, eipPurchaseOrder.getSupplierCode());
        }
        // 采购订单号
        if (StringUtils.isNotEmpty(eipPurchaseOrder.getPoNo())) {
            queryWrapper.like(EipPurchaseOrder::getPoNo, eipPurchaseOrder.getPoNo());
        }
        // 合同编号
        if (StringUtils.isNotEmpty(eipPurchaseOrder.getConCode())) {
            queryWrapper.like(EipPurchaseOrder::getConCode, eipPurchaseOrder.getConCode());
        }// 合同名称
        if (StringUtils.isNotEmpty(eipPurchaseOrder.getConName())) {
            queryWrapper.like(EipPurchaseOrder::getConName, eipPurchaseOrder.getConName());
        }// 采购方公司名称
        if ( StringUtils.isNotEmpty(eipPurchaseOrder.getBuyerName())) {
            if ("蒙东".equals(eipPurchaseOrder.getBuyerName()) || "蒙西".equals(eipPurchaseOrder.getBuyerName()) || "内蒙古".equals(eipPurchaseOrder.getBuyerName())) {
                queryWrapper.like(EipPurchaseOrder::getBuyerName, "蒙东").or().like(EipPurchaseOrder::getBuyerName, "蒙西").or().like(EipPurchaseOrder::getBuyerName, "内蒙古");
            } else if ("冀北".equals(eipPurchaseOrder.getBuyerName()) || "北京".equals(eipPurchaseOrder.getBuyerName())) {
                queryWrapper.like(EipPurchaseOrder::getBuyerName, "冀北").or().like(EipPurchaseOrder::getBuyerName, "北京");
            } else {
                queryWrapper.like(EipPurchaseOrder::getBuyerName, eipPurchaseOrder.getBuyerName());
            }
        }// 采购方物料描述
        if (StringUtils.isNotEmpty(eipPurchaseOrder.getMaterialDesc())) {
            queryWrapper.like(EipPurchaseOrder::getMaterialDesc, eipPurchaseOrder.getMaterialDesc());
        }
        // 合同签订日期
        if (sellerSignTimeStart != null) {
            queryWrapper.ge(EipPurchaseOrder::getSellerSignTime, sellerSignTimeStart);
        }// 合同签订日期
        if (sellerSignTimeEnd != null) {
            queryWrapper.le(EipPurchaseOrder::getSellerSignTime, sellerSignTimeEnd);
        }// 工程项目名称
        if (StringUtils.isNotEmpty(eipPurchaseOrder.getPrjName())) {
            queryWrapper.like(EipPurchaseOrder::getPrjName, eipPurchaseOrder.getPrjName());
        }
        queryWrapper.orderByDesc(EipPurchaseOrder::getSellerSignTime);
        queryWrapper.orderByAsc(EipPurchaseOrder::getPoNo);
        queryWrapper.orderByAsc(EipPurchaseOrder::getPoItemNo);
        return eipPurchaseOrderMapper.selectPage(page, queryWrapper);
    }

}
